﻿function initFolderList() {
    cms.DnD.ClearDropZones();
    // Make the rootMediaFolder the first dropzone
    var rootMediaFolder = document.getElementById("rootMediaFolder");
    cms.DnD.AddDropZone(rootMediaFolder);
    iLib.Dom.AddListener(rootMediaFolder, "mouseover", function (e) {
        if (cms.DnD.GetIsDragging())
            return;
        e = e || window.event;
        var target = e.target || e.srcElement;
        while (target.id != "rootMediaFolder")
            target = target.parentNode;
        if (target.className != "selected")
            target.className = "hover";
    });
    iLib.Dom.AddListener(rootMediaFolder, "mouseout", function (e) {
        if (cms.DnD.GetIsDragging())
            return;
        e = e || window.event;
        var target = e.target || e.srcElement;
        while (target.id != "rootMediaFolder")
            target = target.parentNode;
        if (target.className != "selected")
            target.className = "";
    });
    iLib.Dom.AddListener(rootMediaFolder, "click", function (e) {
        e = e || window.event;
        var target = e.target || e.srcElement;
        while (target.id != "rootMediaFolder")
            target = target.parentNode;
        setSelectedFolder(target);
    });
    // Get all folder spans
    var spans = document.getElementById("folderPane").getElementsByTagName("span");
    for (var i = 0; i < spans.length; i++) {
        if (spans[i].parentNode.tagName == "LI") {
            cms.DnD.AddDropZone(spans[i]);
            iLib.Dom.AddListener(spans[i], "mouseover", function (e) {
                if (cms.DnD.GetIsDragging())
                    return;
                e = e || window.event;
                var target = e.target || e.srcElement;
                while (target.parentNode && target.parentNode.tagName != "LI")
                    target = target.parentNode;
                if (target.className != "selected")
                    target.className = "hover";
            });
            iLib.Dom.AddListener(spans[i], "mouseout", function (e) {
                if (cms.DnD.GetIsDragging())
                    return;
                e = e || window.event;
                var target = e.target || e.srcElement;
                while (target.parentNode && target.parentNode.tagName != "LI")
                    target = target.parentNode;
                if (target.className != "selected")
                    target.className = "";
            });
            iLib.Dom.AddListener(spans[i], "mousedown", function (e) {
                cms.DnD.InitDrag("LI", e);
            });
            iLib.Dom.AddListener(spans[i], "click", function (e) {
                e = e || window.event;
                var target = e.target || e.srcElement;
                setSelectedFolder(target);
            });
        }
    }
    // Get all folder icons
    var imgs = document.getElementById("folderPane").getElementsByTagName("img");
    for (var i = 0; i < imgs.length; i++) {
        if (imgs[i].parentNode.tagName == "SPAN" && imgs[i].parentNode.id != "rootMediaFolder") {
            iLib.Dom.AddListener(imgs[i], "click", function (e) {
                e = e || window.event;
                var target = e.target || e.srcElement;
                iLib.Dom.PreventBubble(e);
                prepareFolderPopup(target, target.parentNode.id, target.nextSibling.textContent, e);
            });
        }
    }
    // Set up the EndDrag handler
    cms.DnD.AddEndDragHandler(onEndDrag);
    // Get all toggle links
    var links = document.getElementById("folderPane").getElementsByTagName("a");
    for (var i = 0; i < links.length; i++) {
        if (links[i].className != "empty") {
            iLib.Dom.AddListener(links[i], "click", function (e) {
                cms.Lists.ShowPaneOverlay("folderPaneOverlay");
                e = e || window.event;
                var target = e.target || e.srcElement;
                iLib.Dom.PreventBubble(e);
                document.getElementById("ctl00_body_hidToggleFolderId").value = target.id.substr(1);
                document.getElementById("ctl00_body_btnToggleFolder").click();
                return false;
            });
        }
    }
}

function setSelectedFolder(target) {
    var spans = document.getElementById("folderPane").getElementsByTagName("span");
    for (var i = 0; i < spans.length; i++)
        spans[i].className = "";
    while (target.parentNode.tagName != "LI" && target.id != "rootMediaFolder")
        target = target.parentNode;
    target.className = "selected";
    var hidSelectedFolderID = document.getElementById("ctl00_body_hidSelectedFolderID");
    if (target.id == hidSelectedFolderID.value)
        return;
    hidSelectedFolderID.value = target.id;
    cms.Lists.ShowPaneOverlay("filePaneOverlay");
    document.getElementById("ctl00_body_btnSelectFolder").click();
}

function onEndDrag(draggedElement, dropzoneElement, dropzonePart) {
    document.getElementById("ctl00_body_hidDraggedID").value = draggedElement.id;
    document.getElementById("ctl00_body_hidDropzoneID").value = dropzoneElement != null ? dropzoneElement.id : "";
    if (dropzoneElement != null && draggedElement.id != dropzoneElement.id) {
        cms.Lists.ShowPaneOverlay("folderPaneOverlay");
        cms.Lists.ShowPaneOverlay("filePaneOverlay");
        // confirmDrag is generated from the code behind to allow localization
        if (confirmDrag) {
            confirmDrag(draggedElement, dropzoneElement);
        }
    }
}

function initFileList() {
    // Get all page spans
    var spans = document.getElementById("filePane").getElementsByTagName("span");
    for (var i = 0; i < spans.length; i++) {
        if (spans[i].parentNode.tagName == "TD" && spans[i].id.indexOf("M") == 0) {
            iLib.Dom.AddListener(spans[i], "mousedown", function (e) { cms.DnD.InitDrag("TD", e); });
        }
    }
    // Get all page icons
    var imgs = document.getElementById("filePane").getElementsByTagName("img");
    for (var i = 0; i < imgs.length; i++) {
        if (imgs[i].parentNode.tagName == "SPAN") {
            iLib.Dom.AddListener(imgs[i], "click", function (e) {
                e = e || window.event;
                var target = e.target || e.srcElement;
                iLib.Dom.PreventBubble(e);
                prepareFilePopup(target, target.parentNode.id, target.nextSibling.textContent, e);
            });
        }
    }
}

function prepareFolderPopup(trigger, id, name, e) {
    var popup = document.getElementById("pnlFolderPopup");
    var hlOpenFolder = document.getElementById("hlOpenFolder");
    hlOpenFolder.onclick = function (e) {
        var target = trigger.nextSibling;
        setSelectedFolder(target);
        return false;
    };
    var hlEditFolder = document.getElementById("hlEditFolder");
    var hidSelectedFolderID = document.getElementById("ctl00_body_hidSelectedFolderID");
    var selectedFolderID = hidSelectedFolderID.value == "rootMediaFolder" ? "-1" : hidSelectedFolderID.value.substring(1);
    var hidBrowseItemData = document.getElementById("ctl00_body_hidBrowseItemData");
    hlEditFolder.href = "Admin/EditMediaFolder.aspx?id=" + id.substring(1) + "&browseitemdata=" + hidBrowseItemData.value;
    document.getElementById("ctl00_body_hidDeleteFolderID").value = id;
    cms.Lists.CurrentName = name;
    cms.Lists.ShowPopup(popup, trigger, "left", 0, 0, true, e);
}

function prepareFilePopup(trigger, id, name, e) {
    var span = trigger.nextSibling;
    var extension = span.innerHTML.substring(span.innerHTML.lastIndexOf(".") + 1);
    var popup = document.getElementById("pnlFilePopup");
    var lbUnzip = document.getElementById("ctl00_body_lbUnzip");
    if (extension.toLowerCase() == "zip")
        lbUnzip.style.display = "";
    else
        lbUnzip.style.display = "none";
    var hidBrowseItemData = document.getElementById("ctl00_body_hidBrowseItemData");
    var hlPixlr = document.getElementById("ctl00_body_hlPixlr");
    if (extension.toLowerCase() == "jpg" || extension.toLowerCase() == "png" || extension.toLowerCase() == "gif") {
        var domain = "http://" + document.getElementById("ctl00_body_hidDomain").value;
        var userID = document.getElementById("ctl00_body_hidUserID").value;
        var folderPath = document.getElementById("ctl00_body_hidSelectedFolderPath").value;
        hlPixlr.href = "Admin/PixlrWrapper.aspx" + "?image=" + domain + "/" + folderPath + "/" + span.innerHTML +
                                    "&target=" + escape(domain + "/PixlrCallbackHandler.aspx?fileid=" + id.substring(1) +
                                                            "&userid=" + userID + "&browseitemdata=" + hidBrowseItemData.value) +
                                    "&browseitemdata=" + hidBrowseItemData.value;
        hlPixlr.style.display = "";
        iLib.Dom.AddListener(hlPixlr, "click", function (e) {
            this.blur();
        });
    }
    else
        hlPixlr.style.display = "none";
    var hlEditFile = document.getElementById("hlEditFile");
    hlEditFile.href = "Admin/EditMediaFile.aspx?id=" + id.substring(1) + "&browseitemdata=" + hidBrowseItemData.value;
    var lbSelect = document.getElementById("ctl00_body_lbSelect");
    var filterCriteriaMet = true;
    var hidBrowseItemFilter = document.getElementById("ctl00_body_hidBrowseItemFilter");
    if (hidBrowseItemFilter.value != "") {
        filterCriteriaMet = false;
        var allowedExtensions = hidBrowseItemFilter.value.split(",");
        for (var i = 0; i < allowedExtensions.length; i++) {
            if (allowedExtensions[i] != null && allowedExtensions[i].toLowerCase() == extension.toLowerCase()) {
                filterCriteriaMet = true;
                break;
            }
        }
    }
    if (hidBrowseItemData.value != "" && filterCriteriaMet)
        lbSelect.style.display = "";
    else
        lbSelect.style.display = "none";
    document.getElementById("ctl00_body_hidSelectedFileID").value = id;
    cms.Lists.CurrentName = name;
    cms.Lists.ShowPopup(popup, trigger, "left", 0, 0, true, e);
}

function toggleAllCheckboxes(checked) {
    $("#filePane input[type=checkbox]").attr("checked", checked);
}

function zipCallback(status) {
    if (status != "ok")
        return;
    cms.Lists.ShowPaneOverlay("folderPaneOverlay");
    cms.Lists.ShowPaneOverlay("filePaneOverlay");
    document.getElementById("ctl00_body_btnStartZipping").click();
}

function captureEnterForZipFilename() {
    iLib.Dom.AddListener(document.getElementById("ctl00_body_txtZip"), "keydown", cms.Dialogs.CaptureEnter);
}

function createPopups() {
    $(".popup").each(function () {
        cms.Lists.CreateListPopupMenu(this);
    });
}

$(document).ready(function () {
    cms.Ajax.Init();
    cms.Ajax.AddEndRequestHandler(function () {
        cms.Lists.HidePaneOverlays();
        createPopups();
    });
    createPopups();
});
